package com.glory.wxmemsicpda.adapter

import android.content.Context
import android.text.Editable
import android.text.InputType
import android.text.TextWatcher
import android.view.View
import android.widget.EditText
import android.widget.FrameLayout
import androidx.annotation.LayoutRes
import com.chad.library.adapter.base.BaseQuickAdapter
import com.glory.wxmemsicpda.R
import com.glory.wxmemsicpda.base.DataBindBaseViewHolder
import com.glory.wxmemsicpda.bean.SheetInfoBean
import com.glory.wxmemsicpda.databinding.AdapterCheckDescBinding
import com.glory.wxmemsicpda.databinding.AdapterCheckDescInspectionBinding
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralPop
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_PopUtils
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StringUtils

/**
 * @Author Zhangjy
 * @Date 2021/6/4 10:41
 * Description :
 */
class CheckDescListAdapter(
    @LayoutRes layoutResId: Int,
    data: List<SheetInfoBean.SheetCheckTableLineEntity?>?,
    s: String?,
    context: Context
) :
    BaseQuickAdapter<SheetInfoBean.SheetCheckTableLineEntity?, DataBindBaseViewHolder>(
        layoutResId,
        data as MutableList<SheetInfoBean.SheetCheckTableLineEntity?>?
    ) {

    private val mCheckTime = s
    private val mResultList: MutableList<String> = mutableListOf("OK", "NG")

    private val mContext: Context = context

    override fun convert(
        helper: DataBindBaseViewHolder,
        item: SheetInfoBean.SheetCheckTableLineEntity?
    ) {
        if (mCheckTime.equals("巡检")) {
            var adapterCheckDescInspectionBinding: AdapterCheckDescInspectionBinding =
                helper.getDataBinding() as AdapterCheckDescInspectionBinding
            adapterCheckDescInspectionBinding.sheetCheckTableLine = item
            adapterCheckDescInspectionBinding.executePendingBindings()

            adapterCheckDescInspectionBinding.tvPosition.text =
                ((helper.adapterPosition + 1).toString())

            if (item!!.datatype.equals("NUM")) {
                adapterCheckDescInspectionBinding.etValue.inputType =
                    InputType.TYPE_CLASS_NUMBER or InputType.TYPE_NUMBER_FLAG_SIGNED or InputType.TYPE_NUMBER_FLAG_DECIMAL
                try {
                    compare(
                        item.value.toDouble(),
                        item.maxvalue,
                        item.minvalue,
                        adapterCheckDescInspectionBinding.etValue
                    )
                } catch (e: NumberFormatException) {
                    e.printStackTrace()
                }
            }

            setEditListener(adapterCheckDescInspectionBinding.etValue, item, "Y")
//            setEditListener(adapterCheckDescInspectionBinding.etComment, item, "N")

            adapterCheckDescInspectionBinding.flResultList.setOnClickListener {
                 var mResultPop: FreeUI_GeneralPop? = FreeApi_PopUtils.getNormalPop(
                    mContext,
                    adapterCheckDescInspectionBinding.flResultList.width,
                    mResultList,
                    adapterCheckDescInspectionBinding.ivResultArrow
                ) { code: String?, i: Int ->
                    item.reserveD8 = code
                     adapterCheckDescInspectionBinding.tvResult.text = code
                }
                FreeApi_PopUtils.setArrowShow(adapterCheckDescInspectionBinding.ivResultArrow)
                mResultPop?.showAsDropDown(adapterCheckDescInspectionBinding.flResultList, 0, 0)
            }

        } else {
            var adapterCheckDescBinding: AdapterCheckDescBinding =
                helper.getDataBinding() as AdapterCheckDescBinding
            adapterCheckDescBinding.sheetCheckTableLine = item
            adapterCheckDescBinding.executePendingBindings()

            adapterCheckDescBinding.tvPosition.text = ((helper.adapterPosition + 1).toString())

            if (item!!.datatype.equals("NUM")) {
                adapterCheckDescBinding.etValue.inputType =
                    InputType.TYPE_CLASS_NUMBER or InputType.TYPE_NUMBER_FLAG_SIGNED or InputType.TYPE_NUMBER_FLAG_DECIMAL
                try {
                    compare(
                        item.value.toDouble(),
                        item.maxvalue,
                        item.minvalue,
                        adapterCheckDescBinding.etValue
                    )
                } catch (e: NumberFormatException) {
                    e.printStackTrace()
                }
            }

            setEditListener(adapterCheckDescBinding.etValue, item, "Y")
        }

    }

    private fun setEditListener(
        editText: EditText,
        item: SheetInfoBean.SheetCheckTableLineEntity?,
        type: String
    ) {
        if (editText.tag is TextWatcher) {
            editText.removeTextChangedListener(editText.tag as TextWatcher);
        }

        var textWatcher: TextWatcher = object : TextWatcher {

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}

            override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
                if (s.isNotEmpty()) {
                    if (type == "Y") {
                        if (item?.datatype.equals("NUM")) {
                            try {
                                val value = s.toString().toDouble()
                                compare(value, item!!.maxvalue, item.minvalue, editText)
                            } catch (e: java.lang.NumberFormatException) {
                                e.printStackTrace()
                            }
                        }
                    }
                }
            }

            override fun afterTextChanged(s: Editable) {
                if (type == "Y") {
                    item?.value = s.toString()
                } else {
                    item?.comments = s.toString()
                }
            }
        }

        editText.addTextChangedListener(textWatcher);
        editText.tag = textWatcher;
    }


    private fun compare(value: Double, maxValue: String, minValue: String, editText: EditText) {
        if (!FreeApi_StringUtils.isEmpty(maxValue) && !FreeApi_StringUtils.isEmpty(minValue)) {
            val maxValues = maxValue.toDouble()
            val minValues = minValue.toDouble()
            if (value in minValues..maxValues) {
                editText.setTextColor(mContext.resources.getColor(R.color.black))
            } else {
                editText.setTextColor(mContext.resources.getColor(R.color.red))
            }
        } else if (FreeApi_StringUtils.isEmpty(maxValue) && !FreeApi_StringUtils.isEmpty(minValue)) {
            val minValue1 = minValue.toDouble()
            if (value >= minValue1) {
                editText.setTextColor(mContext.resources.getColor(R.color.black))
            } else {
                editText.setTextColor(mContext.resources.getColor(R.color.red))
            }
        } else if (!FreeApi_StringUtils.isEmpty(maxValue) && FreeApi_StringUtils.isEmpty(minValue)) {
            val maxValue1 = maxValue.toDouble()
            if (value <= maxValue1) {
                editText.setTextColor(mContext.resources.getColor(R.color.black))
            } else {
                editText.setTextColor(mContext.resources.getColor(R.color.red))
            }
        } else {
            editText.setTextColor(mContext.resources.getColor(R.color.black))
        }
    }


}